package com.woniu.service.impl;

import com.woniu.entity.FlowerInfo;
import com.woniu.mapper.FlowerCartMapper;
import com.woniu.mapper.FlowerInfoMapper;
import com.woniu.service.FlowerInfoServiceCZT;
import com.woniu.vo.CartVoCZT;
import com.woniu.vo.FlowerInfoVoCZT;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class FlowerInfoServiceCZTImpl implements FlowerInfoServiceCZT {
    @Resource
    private FlowerInfoMapper fim;

    @Resource
    private FlowerCartMapper fcm;

    @Override
    public int checkFlowerInfoCount(int[] cartIds) {
        int i = 0;
        for (int cartId : cartIds) {
            CartVoCZT cartVoCZT = fcm.selectCartByCartId(cartId);
            int buyCount = cartVoCZT.getBuyCount();

            int flowerId = cartVoCZT.getFlowerId();
            Example example = new Example(FlowerInfo.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("flowerId", flowerId);
            FlowerInfo flowerInfo = fim.selectOneByExample(example);
            Integer count = flowerInfo.getFlowerStoreCount();
            if (buyCount <= count) {
                i = 1;
            } else {
                i = 0;
                break;
            }
        }
        return i;
    }

    @Override
    public FlowerInfoVoCZT selectFlowerInfoByFlowerId(int flowerId) {
        return fim.selectFlowerInfoByFlowerId(flowerId);
    }

    @Override
    public List<FlowerInfo> selectFlowerBySale() {
        Example example = new Example(FlowerInfo.class);
        example.orderBy("flowerSaleCount").desc();
        example.orderBy("flowerId").desc();
        List<FlowerInfo> select = fim.selectByExample(example);
        ArrayList<FlowerInfo> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            list.add(select.get(i));
        }
        return list;
    }

}
